/**
 * The transport system is the main model, which is used to load/unload goods to
 * container. In addition, it is possible to get the container's current weight
 * and the goods with the highest value.
 * 
 * @author Xi
 * 
 */
public class TransportSystem {
	private static final int CONTAINER1 = 1;
	private static final int CONTAINER2 = 2;
	private static final int CONTAINER3 = 3;
	private static final int NORMAL = 1;
	private static final int FROZEN = 2;

	/**
	 * The given container loads the given goods.
	 * 
	 * @param container
	 * @param goods
	 */
	public void load(Container container, Goods goods) {
		container.load(goods);
	}

	/**
	 * The given container unloads the goods.
	 * 
	 * @param container
	 */
	public void unload(Container container) {
		container.unload();
	}

	/**
	 * Get the weight of all loaded goods.
	 * 
	 * @param container
	 * @return the weight of goods in the given container
	 */
	public int getWeight(Container container) {
		return container.getWeight();
	}

	/**
	 * Get the goods with the highest value from the container.
	 * 
	 * @param container
	 * @return the goods with the highest value from the container
	 */
	public Goods getHighestValuedGoods(Container container) {
		Goods highestValuedGoods = null;
		int value = 0;
		for (Goods goods : container.getGoodsList()) {
			if (value <= goods.getValue()) {
				value = goods.getValue();
				highestValuedGoods = goods;
			}
		}
		if (highestValuedGoods != null) {
			System.out
					.println("Return the goods with the highest value, goods weight: "
							+ highestValuedGoods.getWeight()
							+ " , goods type: "
							+ highestValuedGoods.getType()
							+ " , goods value: "
							+ highestValuedGoods.getValue());
		} else {
			System.out.println("There is no goods in the container!");
		}
		return highestValuedGoods;
	}

	/**
	 * Create a new container with the given parameters
	 * @param capacity 
	 * @param containerType 1: container1; 2: container2; 3: container3
	 * @return
	 * @throws TransportException
	 */
	public Container createContainer(int capacity, int containerType)
			throws TransportException {
		Container continer = null;
		switch (containerType) {
		case CONTAINER1:
			continer = new Container1(capacity);
			break;
		case CONTAINER2:
			continer = new Container2(capacity);
			break;
		case CONTAINER3:
			continer = new Container3(capacity);
			break;
		}
		if (continer == null) {
			throw new TransportException("container type is wrong!");
		}
		return continer;
	}

	/**
	 * Create a new goods with the given parameters.
	 * @param weight 
	 * @param goodsType
	 * @param value
	 * @return
	 * @throws TransportException
	 */
	public Goods createGoods(int weight, int goodsType, int value)
			throws TransportException {
		Goods goods = null;
		switch (goodsType) {
		case NORMAL:
			goods = new Goods(weight, GoodsType.Normal, value);
			break;
		case FROZEN:
			goods = new Goods(weight, GoodsType.Frozen, value);
			break;
		}
		if (goods == null) {
			throw new TransportException("goods type is wrong!");
		}
		return goods;
	}
}
